/**
 *  Copyright (C) 2006 zhangbo (freeeob@gmail.com)
 *
 *  This product is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation; either version 2.1 of the License, or
 *  (at your option) any later version.
 * 
 *  This product is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 * 
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 *
 *  author:zhangbo
 *  Email:jsjava@gmail.com
 */

/**
 * constructor
 * @param id
 * @param capacity the number of the menu being able to contain menutiems 
 */
function HTMLMenu(id,capacity){
	this.jsjava_class="jsorg.eob.component.menu.HTMLMenu";
	this.id=id;                        
	this.capacity=capacity;            
	this.children=new Array(capacity); 
	this.size=0;                       
	this.isLeaf=false;
	if(capacity==0)
	{
	   this.isLeaf=true;
	}  
	this.parent=null;
	this._isURLForbid=false;
	this._isScriptForbid=false;
	this._isPassURLScriptArgsOnly=false;
}
   
/**
 * set menu title
 * @param title
 */
HTMLMenu.prototype.setTitle=function (title){
   this.title=title;
};  

/**
 * get menu title
 */
HTMLMenu.prototype.getTitle=function(){
   return this.title;
};  

/**
 * add a menuitem
 * @param childNode a menu object
 */
HTMLMenu.prototype.addChild=function (childNode){
   this.children[this.size]=childNode;
   this.size=this.size+1;
   childNode.parent=this;
};   

/**
 * get all child menuitems
 */     
HTMLMenu.prototype.getChildren=function (){
   var children=new Array(this.size);
   for(var i=0;i<children.length;i++)
   {
      children[i]=this.children[i];
   }
   return children;
};    

/**
 * get menu's parent menu
 */  
HTMLMenu.prototype.getParent=function (){
   return this.parent;
};   

/**
 * set menu display icon
 * @param icon a image url string
 */
HTMLMenu.prototype.setIcon=function (icon){
   this.icon=icon;
};    

/**
 * return menu icon
 */
HTMLMenu.prototype.getIcon=function (){
   return this.icon;
};  

/**
 * set menu default icon
 * @param defaultIcon
 */            
HTMLMenu.prototype.setDefaultIcon=function (defaultIcon){
   this.defaultIcon=defaultIcon;
};

/**
 * return menu default icon
 */
HTMLMenu.prototype.getDefaultIcon=function (){
   return this.defaultIcon;
};

/**
 * set menu link
 * @param url a url string 
 */
HTMLMenu.prototype.setLink=function (url){
   this.url=url;
};    

/**
 * return menu link
 */  
HTMLMenu.prototype.getLink=function (){
   return this.url;
};      

/**
 * set menu link target such as _blank _top and so on
 * @param target
 */  
HTMLMenu.prototype.setTarget=function (target){
   this.target=target;	
};   

/**
 * get menu link target
 */
HTMLMenu.prototype.getTarget=function (){
   return this.target;	
};    

/**
 * set features of the window of the linked page such as menubar=no status=no and so on
 * @param title
 */      
HTMLMenu.prototype.setFeature=function (feature){
   this.feature=feature;	
};

/**
 * get features of the window of the linked page
 */
HTMLMenu.prototype.getFeature=function (){
   return this.feature;	
};

/**
 * set menu script function name on clicking the menu
 * @param scriptName javascript function name of the page
 */
HTMLMenu.prototype.setScriptName=function (scriptName){
   this.scriptName=scriptName;	
};  

/**
 * get menu script function name
 */
HTMLMenu.prototype.getScriptName=function (){
   return this.scriptName;	
};  

/**
 * set menu script function arguments
 * @param scriptArgs 
 */
HTMLMenu.prototype.setScriptArgs=function (scriptArgs){
   this.scriptArgs=scriptArgs;	
};  

/**
 * get menu script function arguments
 */
HTMLMenu.prototype.getScriptArgs=function (){
   return this.scriptArgs;	
};  	

/**
 * set script function on mouse over the menu
 * @param mouseover
 */
HTMLMenu.prototype.setMouseover=function (mouseover){
   this.mouseover=mouseover;	
};

/**
 * get script function on mouse over the menu
 */
HTMLMenu.prototype.getMouseover=function (){
   return this.mouseover;
};	

/**
 * set script function on mouse out of he menu
 * @param mouseout
 */
HTMLMenu.prototype.setMouseout=function (mouseout){
   this.mouseout=mouseout;	
};

/**
 * get script function on mouse out of the menu
 */
HTMLMenu.prototype.getMouseout=function (){
   return this.mouseout;
};

/**
 * set menu background color
 * @param background a color
 */
HTMLMenu.prototype.setBackground=function (background){
   this.background=background;	
};

/**
 * get menu background color
 */
HTMLMenu.prototype.getBackground=function (){
   return this.background;	
};

/**
 * set the boolean value whether rendering the menu display on mouse over the menu
 * @param isRender a boolean value
 */
HTMLMenu.prototype.setMouseRender=function (isRender){
   this.mouseRender=isRender;	
};

/**
 * get the boolean value whether rendering the menu display on mouse over the menu
 */
HTMLMenu.prototype.isMouseRender=function(){
   return this.mouseRender;	
};

/**
 * return the display HTML content
 */
HTMLMenu.prototype.getContent=function (){
	var topNode=this.getTop();
     var content="<table width='100%' cellpadding='3' cellspacing='0' borderColor='#316AC5' class='WindowMenu' background='"+this.getBackground()+"'>";
 	for(var i=0;i<this.size;i++){
 	    var child=this.children[i];
 	    var linkUrl=child.getLink();
 	    var linkTarget=child.getTarget();
 	    var clickStr=child.getScriptName();
 	    var scriptArgs=child.getScriptArgs();
 	    if(clickStr==null||clickStr==""){
 	        clickStr=topNode.getScriptName();	
 	        scriptArgs=topNode.getScriptArgs();
 	    }
 	    var cursor="default";
 	    if(clickStr==null||clickStr==undefined){
 	    	clickStr="";
 	    }else{    	        
 	        if(topNode.isPassURLScriptArgsOnly()){
 	            clickStr+="(\""+linkUrl+"\","+"\""+linkTarget+"\")";	
 	        }else{
 	            clickStr+="("+scriptArgs+")";
 	        }
 	        cursor="hand";
 	    }
 	    var linkLeftStr="";
 	    var linkRightStr="";
 	    
 	    var isURLForbid=topNode.isURLForbid();
 	    if(linkUrl!=null&&linkUrl!=undefined&&!isURLForbid){
 	    	if(linkTarget==null||linkTarget==undefined){
 	    	    linkTarget="_self";	
 	    	}
 	    	var openStr="\""+linkUrl+"\",\""+linkTarget+"\","+"\"\"";
 	        linkLeftStr="<span style='text-decoration:none;cursor:hand' onclick='window.open("+openStr+")'>";
 	        linkRightStr="</span>";
 	    }
 	    var icon=child.getIcon();
 	    if(icon==null||icon==undefined){
 	        icon=topNode.getDefaultIcon();
 	    }
 	    content+="<tr>";
 	    content+="<td id='node_0_"+this.id+"' width='1' valign='top'>";
 	    content+="<img id='node_0_1"+this.id+"' src='"+icon+"' nowrap/>";
 	    content+="</td>";
 	    content+="<td id='node_1_"+this.id+"' nowrap valign='middle' style='cursor:"+cursor+"' onclick='"+clickStr+"'";
 	    if(this.isMouseRender()){
 	    	content+=" onmouseover='parentElement.className=\"WindowMenuMouseOver\"' onmouseout='parentElement.className=\"WindowMenuMouseOut\"'";
 	    }
 	    content+=">";
 	    content+=linkLeftStr+child.getTitle()+linkRightStr;
 	    content+="</td>";
 	    content+="</tr>";
 	}    
 	content+="</table>";
 	return content;
};

/**
 * get menu's root menu
 */
HTMLMenu.prototype.getTop=function (){
   var parentNode=this.getParent();
   if(parentNode==null||parentNode==undefined){
   	 return this;
   }
   return parentNode.getTop();
};

/**
 * forbid url link operation
 */
HTMLMenu.prototype.forbidURL=function(){
    this._isURLForbid=true;	
};

/**
 * allow url link operation
 */
HTMLMenu.prototype.enableURL=function(){
    this._isURLForbid=false;	
};

/**
 * return the boolean value whether forbidding url link operation
 */
HTMLMenu.prototype.isURLForbid=function(){
    return this._isURLForbid;
};

/**
 * forbid script event operation
 */
HTMLMenu.prototype.forbidScript=function(){
    this._isScriptForbid=true;	
};

/**
 * allow script event operation
 */
HTMLMenu.prototype.enableScript=function(){
    this._isScriptForbid=false;	
};

/**
 * return the boolean value whether allowing script event operation
 */
HTMLMenu.prototype.isScriptForbid=function(){
    return this._isScriptForbid;	
};

/**
 *  only allow pass url link and target args to the javascript function
 */
HTMLMenu.prototype.passURLScriptArgsOnly=function(){
    this._isPassURLScriptArgsOnly=true;
};

/**
 * return the boolean value whether only allowing pass url link and target 
 * args to the javascript function
 */
HTMLMenu.prototype.isPassURLScriptArgsOnly=function(){
    return this._isPassURLScriptArgsOnly;
};

/**
 *  init the menu capacity
 */
HTMLMenu.prototype.init=function (capacity){
   this.capacity=capacity;
};        
   